<template>
  <div>
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入企业名称"
      v-model="nickname"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入招聘岗位名称"
      v-model="name"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入工作城市"
      v-model="city"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入招聘人数"
      v-model="number"
    />
    <el-date-picker
      style="float: left; margin-left: 15px"
      v-model="benginEndTime"
      unlink-panels
      value-format="yyyy-MM-dd"
      size="small"
      type="daterange"
      range-separator="至"
      start-placeholder="请选择发布日期"
      end-placeholder="请选择截止日期"
    >
    </el-date-picker>
    <el-input
      style="width: 200px; float: left; margin-top: 10px; margin-left: 15px"
      size="small"
      placeholder="请输入最低薪资"
      v-model="minSal"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px; margin-top: 10px"
      size="small"
      placeholder="请输入最高薪资"
      v-model="maxSal"
    />
    <el-select
      v-model="status"
      style="width: 200px; float: left; margin-left: 15px; margin-top: 10px"
      size="small"
      clearable
      placeholder="请选择审核状态"
    >
      <el-option
        v-for="item in options"
        :key="item"
        :label="item"
        :value="item"
      >
      </el-option>
    </el-select>
    <el-button
      type="primary"
      icon="el-icon-search"
      style="float: left; margin-left: 15px; margin-top: 10px"
      size="small"
      @click="initRecruitInfo"
      >搜索</el-button
    >
    <el-button
      style="float: left; margin-left: 15px; margin-top: 10px"
      icon="el-icon-refresh"
      size="small"
      @click="clearInput"
      >重置</el-button
    >
    <el-button
      style="
        float: left;
        margin-top: 10px;
        margin-right: 15px;
        margin-left: 15px;
      "
      type="danger"
      plain
      icon="el-icon-delete"
      size="small"
      @click="delAll"
      :disabled="this.multipleSelection.length == 0"
      >批量删除</el-button
    >
    <el-button
      type="warning"
      plain
      size="small"
      icon="el-icon-upload2"
      style="
        float: left;
        margin-top: 10px;
        margin-left: 0px;
        margin-bottom: 10px;
      "
      @click="exportReInfo"
      >导出</el-button
    >
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      :cell-style="rowClass"
      :header-cell-style="headClass"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45"> </el-table-column>
      <el-table-column prop="enterprise.nickname" label="企业名称" width="100">
        <template slot-scope="scope"
          ><el-popover placement="bottom" width="150" trigger="hover"
            ><div>
              <h4>企业logo</h4>
              <img style="width: 150px" :src="scope.row.enterprise.logo" />
              <h4>企业描述</h4>
              <font style="font-size: 10px">{{
                scope.row.enterprise.description
              }}</font>
            </div>
            <el-tag slot="reference">{{
              scope.row.enterprise.nickname
            }}</el-tag></el-popover
          ></template
        >
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="name"
        label="岗位名称"
        width="80"
      >
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="jobDuty"
        label="岗位职责"
        width="80"
      >
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="demand"
        label="岗位要求"
        width="80"
      >
      </el-table-column>
      <el-table-column prop="city" label="工作城市" width="100">
      </el-table-column>
      <el-table-column prop="publishTime" label="发布日期" width="100">
      </el-table-column>
      <el-table-column prop="deadline" label="截至日期" width="100">
      </el-table-column>
      <el-table-column prop="number" label="招聘人数" width="100">
      </el-table-column>
      <el-table-column prop="minSal" label="最低薪资" width="100">
      </el-table-column>
      <el-table-column prop="maxSal" label="最高薪资" width="100">
      </el-table-column>
      <el-table-column prop="status" label="审核状态" width="100">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '审核中'"
            size="mini"
            @click="handleEdit(scope.row)"
            >审核</el-button
          >
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 1.5%; float: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="currentPage"
        :page-sizes="[5, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="修改状态"
      :visible.sync="editDialogVisible"
      width="400px"
      center
    >
      <el-form
        :model="editData"
        ref="editForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item prop="status" label="岗位状态">
          <el-select
            v-model="editData.status"
            clearable
            placeholder="请选择岗位状态"
          >
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" type="primary" @click="updateReStatus"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      name: "",
      nickname: "",
      benginEndTime: "",
      number: "",
      city: "",
      minSal: "",
      maxSal: "",
      status: "",
      multipleSelection: [],
      options: ["审核中", "通过", "驳回"],
      dialogVisible: false,
      editDialogVisible: false,
      editData: {
        id: null,
        status: "",
      },
      rules: {
        status: [
          { required: true, message: "请选择岗位状态", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.initRecruitInfo();
  },
  methods: {
    clearInput() {
      location.reload();
    },
    // 表格样式设置
    rowClass() {
      return "text-align: center;";
    },
    headClass() {
      return "text-align: center;background:#f8f8f9;";
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initRecruitInfo();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.initRecruitInfo();
    },
    exportReInfo() {
      this.$downloadReq("/recruit/export");
    },
    handleDelete(row) {
      this.$confirm(
        '此操作将删除ID为"' + row.id + '"的数据, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        if (row.status == "驳回") {
          this.$delete("recruit/?ids=" + row.id).then((res) => {
            if (res.code == 200) {
              this.initRecruitInfo();
            }
          });
        } else {
          this.$message.error("只能删除审核状态为驳回的数据");
        }
      });
    },
    delAll() {
      this.$confirm(
        '此操作将删除"' + this.multipleSelection.length + '"个职位, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let ids = "?";
          this.multipleSelection.forEach((item) => {
            if (item.status != "驳回") {
              this.$message.error("所选数据中有审核状态不为驳回的数据");
              throw Error();
            } else {
              ids += "ids=" + item.id + "&";
            }
          });
          this.$delete("recruit/" + ids).then((res) => {
            if (res.code == 200) {
              this.initRecruitInfo();
            }
          });
        })
        .catch(() => {
          this.initRecruitInfo();
        });
    },
    updateReStatus() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          this.$put("recruit/", this.editData).then((res) => {
            if (res.code == 200) {
              this.initRecruitInfo();
              this.editDialogVisible = false;
            }
          });
        } else {
          this.$message.error("数据校验失败，请检查");
        }
      });
    },
    handleEdit(row) {
      this.editData.id = row.id;
      this.editData.status = row.status;
      this.editDialogVisible = true;
    },
    initRecruitInfo() {
      this.$get(
        "recruit/" +
          this.currentPage +
          "/" +
          this.pageSize +
          "?nickname=" +
          this.nickname +
          "&name=" +
          this.name +
          "&number=" +
          this.number +
          "&city=" +
          this.city +
          "&status=" +
          this.status +
          "&minSal=" +
          this.minSal +
          "&maxSal=" +
          this.maxSal +
          "&beginEndTime=" +
          this.benginEndTime
      ).then((res) => {
        if (res.code == 200 && res.code) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
  },
};
</script>

<style>
</style>